package com.platform.api;

import com.platform.annotation.LoginUser;
import com.platform.entity.ApiAmountBillVo;
import com.platform.entity.UserInfo;
import com.platform.entity.UserVo;
import com.platform.service.ApiAmountBillService;
import com.platform.service.ApiUserService;
import com.platform.util.UtilServiceName;
import com.platform.utils.ApiRRException;
import com.platform.utils.IdUtil;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 窝币账单流水
 *
 * @author zhurusen
 * @email 524227263@qq.com
 */
@RestController
@RequestMapping("/api/amount")
public class ApiAmountBillController {

    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private ApiAmountBillService apiAmountBillService;

    /**
     * 充值开始生产 订单号
     *
     * @param loginUser
     * @param amount
     * @param channel
     * @return
     */
    @RequestMapping("recharge")
    public R recharge(@LoginUser UserVo loginUser, BigDecimal amount, String channel) {

        if (loginUser.getId() == 1) {
            return R.ok(401, "请先登录", "");

        }


        String billNo = UUID.randomUUID().toString().replace("-", "");
        ApiAmountBillVo apiAmountBillVo = new ApiAmountBillVo();

        apiAmountBillVo.setCreateTime(new Date());
        apiAmountBillVo.setMouldId(loginUser.getId());
        apiAmountBillVo.setAmount(amount);
        apiAmountBillVo.setUserId(loginUser.getId());
        apiAmountBillVo.setTitle("充值");
        apiAmountBillVo.setMouldName(channel);
        apiAmountBillVo.setChannel(channel);
        apiAmountBillVo.setStatus(1);
        apiAmountBillVo.setType(2);
        apiAmountBillVo.setBillNo(billNo);
        apiAmountBillService.save(apiAmountBillVo);

        return R.ok(0, "请求成功", billNo);
    }

    /**
     * 充值成功后回调
     *
     * @param loginUser
     * @param billNo
     * @return
     */
    @RequestMapping("updataRecharge")
    public R updataRecharge(@LoginUser UserVo loginUser, String billNo) {
        ApiAmountBillVo apiAmountBillVo = apiAmountBillService.selecyByBillNo(billNo);
        if (apiAmountBillVo != null) {
            apiAmountBillVo.setStatus(3);
            apiAmountBillService.updata(apiAmountBillVo);
            apiAmountBillService.recharge(apiAmountBillVo);
        }
        UserVo userVo = apiUserService.queryObject(loginUser.getId());
        return R.ok(0, "请求成功", userVo.getAvailable_amount());
    }


    /**
     * IOS充值专用
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("iosRecharge")
    public R iosRecharge(@LoginUser UserVo loginUser, BigDecimal amount) {

        loginUser.setAvailable_amount(loginUser.getAvailable_amount().add(amount));
        loginUser.setWithdraw_amount(loginUser.getWithdraw_amount().add(amount));

        apiUserService.update(loginUser);

        ApiAmountBillVo apiAmountBillVo = new ApiAmountBillVo();
        apiAmountBillVo.setCreateTime(new Date());
        apiAmountBillVo.setMouldId(loginUser.getId());
        apiAmountBillVo.setAmount(amount);
        apiAmountBillVo.setUserId(loginUser.getId());
        apiAmountBillVo.setTitle("充值");
        apiAmountBillVo.setMouldName("iospay");
        apiAmountBillVo.setChannel("iospay");
        apiAmountBillVo.setStatus(1);
        apiAmountBillVo.setType(2);
        apiAmountBillVo.setBillNo("0000000000000000");
        apiAmountBillService.save(apiAmountBillVo);


        UserVo userVo = apiUserService.queryObject(loginUser.getId());
        return R.ok(0, "请求成功", userVo.getAvailable_amount());
    }

    /**
     * 消费
     *
     * @param loginUser
     * @param amount
     * @param serialNumber
     * @param mouldId      消费对象
     * @param mouldName    live (一对一视频)、
     *                     wake（叫醒）、
     *                     emotion（情感） 、
     *                     guide（导游） 、
     *                     circleReward（探索打赏）、
     *                     videoReward（小视频打赏）、
     *                     videoPay(小视频解锁)
     *                     indexReward（主播主页打赏）
     *                     liveReward(一对一视频打赏)
     * @return
     */
    @RequestMapping("save")
    public R save(@LoginUser UserVo loginUser, BigDecimal amount, String serialNumber, Long mouldId, String mouldName) {

        if (amount.compareTo(loginUser.getAvailable_amount()) > 0) {
            return R.ok(1, "你的余额不足，请先充值", loginUser.getAvailable_amount().stripTrailingZeros().toPlainString());
        }

        apiAmountBillService.consume(mouldId, mouldName, amount, loginUser);

        UserVo userVo = apiUserService.queryObject(loginUser.getId());
        return R.ok(0, "支付成功", userVo.getAvailable_amount().stripTrailingZeros().toPlainString());
    }

    /**
     * 收入、支出明细
     *
     * @param loginUser
     * @param type
     * @param page
     * @return
     */
    @RequestMapping("amountList")
    public R amountList(@LoginUser UserVo loginUser, Integer type, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map params = new HashMap();
        params.put("limit", 10);
        params.put("page", page);
        params.put("status", 3);

        params.put("sidx", "create_time");
        params.put("order", "desc");
        params.put("type", type);

        params.put("userId", loginUser.getId());

        params.put("sidx", "create_time");
        params.put("order", "desc");
        Query query = new Query(params);

        List<ApiAmountBillVo> list = apiAmountBillService.queryList(query);

        List<Map<String, Object>> reObject = new ArrayList<>();
        for (ApiAmountBillVo info : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("createTime", info.getCreateTime());
            map.put("amount", info.getAmount());
            map.put("mouldName", UtilServiceName.serviceNmae(info.getMouldName()));

            UserVo userVo = apiUserService.queryObject(info.getMouldId());
            if (userVo != null) {
                UserInfo userInfo = new UserInfo();
                userInfo.setAvatar(userVo.getAvatar());
                userInfo.setNickName(userVo.getNickname());
                map.put("userInfo", userInfo);
            }
            reObject.add(map);
        }

        return R.ok(0, "获取成功", reObject);
    }


}
